package com.yjc.hire.server.mq;


import com.rabbitmq.client.Channel;
import com.yjc.hire.mq.RabbitMqSMSConfig;
import com.yjc.hire.mq.RabbitMqSMSConfig_Dead;
import com.yjc.hire.utils.JsonUtils;
import com.yjc.mq.SMSContentQO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.yjc.hire.base.BaseInfoProperties.MOBILE_SMSCODE;


/**
 * 死信队列的监听者
 */
@Slf4j
@Component
public class RabbitMQSMSConsumer_Dead {
    @Autowired
    private StringRedisTemplate redisTemplate;
//    @RabbitListener(queues = {RabbitMqSMSConfig.SMS_QUEUE})
//    public void watchQueue(String payload, Message message){
//        log.info("payload:{}",payload);
//        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
//        log.info("routingKey:{}",routingKey);
//        String mag = payload;
//        log.info("mag:{}",mag);
//        if (routingKey.equalsIgnoreCase(RabbitMqSMSConfig.ROUTING_KEY_SNS_SEND_LOGIN)){
//            SMSContentQO qo = JsonUtils.jsonToObject(mag, SMSContentQO.class);
//            String code = qo.getContent();
//            String mobile = qo.getMobile();
//            redisTemplate.opsForValue().set(MOBILE_SMSCODE + ":" + mobile, code,30 * 60, TimeUnit.SECONDS);
//        }
//    }

    @RabbitListener(queues = {RabbitMqSMSConfig_Dead.SMS_QUEUE_DEAD})
    public void watchQueue(Channel channel, Message message) throws IOException {
        log.info("+++++++++++++");
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey:{}",routingKey);
        String mag = new String(message.getBody());
        log.info("mag:{}",mag);
            //deliverTag; 消息投递的标签
            //multiple 批量确认所有消费者获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            log.info("++++++++++++++++");

    }
}
